<?php
namespace App\Http\Controllers\AppApi\Msg;

use App\Services\Main\MsgGroupService;
use Illuminate\Http\Request;

class GroupController extends BaseController
{
    /**
     * 内部消息
     * 制作者：九哥
     */

    protected $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new MsgGroupService();
    }

    /**
     * 群消息列表
     */
    public function getIndex(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        if (!$token) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getIndexByUid($uid);
        if (!is_array($models) || !count($models)) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }

    /**
     * 群消息获取
     */
    public function getDetails(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $groupId = $request->groupId;
        if (!$token || !$groupId) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getDetails($uid,$groupId);
        if (!$models) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }

    /**
     * 群消息发送
     */
    public function setSend(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $groupId = $request->groupId;
        $quest = $request->quest;
        if (!$token || !$groupId || !$quest) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $this->service->setSend($groupId,$uid,$quest);
        return $this->toJson(0);
    }

    /**
     * 群友列表
     */
    public function getStaffsById(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $groupId = $request->groupId;
        if (!$token || !$groupId) { return $this->toJson(-2); }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->service->getStaffs($groupId);
        if (!$models) { return $this->toJson(-3); }
        return $this->toJson(0,$models);
    }
}
